import { useStore,createNamespacedHelpers} from 'vuex';
import { computed } from 'vue';
export function useStoreMapper(mapper,mapFn) {
  const store = useStore();
  const storeStateFns=mapFn(mapper);
  const storeState = {};
  Object.keys(storeStateFns).forEach((getter) => {
    //vuex源码中mapState和mapGetters的方法中使用的是this.$store,所以更改this绑定
    storeState[getter] = computed(() =>storeStateFns[getter].bind({$store:store}));
  })
  return storeState;
}
const checkType=(data)=>Object.prototype.toString.call(data).slice(8, -1);
export const useStoreFns=(moduleName,mapFn,mapFnName)=>{
  let mapperFn=mapFn;
  if(checkType(moduleName)==='String'&&moduleName.length){
    mapperFn=createNamespacedHelpers(moduleName)[mapFnName];
  }
  return mapperFn;
}